//
//  BaseHttpClient.m
//  OAProject
//
//  Created by yhj on 2017/2/21.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "BaseHttpClient.h"
#import <AFNetworking.h>

@implementation BaseHttpClient

/**
 *  发起网络请求
 *
 *  @param path       请求接口URI
 *  @param method     请求类型(GET/POST)
 *  @param parameters API参数字典
 *  @param success    成功block
 *  @param failure    失败block
 */
- (void)requestPath:(NSString *)path
             method:(NSString *)method
         parameters:(NSDictionary *)parameters
            success:(void (^)(NSURLSessionDataTask *operation, id responseObject))success
            failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure
{
    NSParameterAssert(path);
    NSParameterAssert(method);

    // GET
    if ([method isEqualToString:@"GET"])
    {
//      [self GET:path parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
//
//      } success:success failure:failure];
        [self GET:path parameters:parameters success:success failure:failure];
    }
    // POST
    else if ([method isEqualToString:@"POST"])
    {
//       [self POST:path parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
//
//        } success:success failure:failure];

        [self POST:path parameters:parameters success:success failure:failure];
    }
    // PUT
    else if ([method isEqualToString:@"PUT"])
    {
        [self PUT:path parameters:parameters success:success failure:failure];
    }
    // DELETE
    else if ([method isEqualToString:@"DELETE"])
    {
        [self DELETE:path parameters:parameters success:success failure:failure];
    }
    // PATCH
    else if ([method isEqualToString:@"PATCH"])
    {
        [self PATCH:path parameters:parameters success:success failure:failure];
    }
}

/**
 *  发起网络请求(带二进制数据)
 *
 *  @param path             请求接口URI
 *  @param method           请求类型(GET/POST)
 *  @param parameters       API参数字典
 *  @param dataParameters    Data参数字典
 *  @param success           成功block
 *  @param failure           失败block
 */
- (void)requestPath:(NSString *)path
//             method:(NSString *)method
         parameters:(NSDictionary *)parameters
     dataParameters:(NSDictionary *)dataParameters
            success:(void (^)(NSURLSessionDataTask *operation, id responseObject))success
            failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure
{
    NSParameterAssert(path);
    [self POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [dataParameters enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            [formData appendPartWithFileData:obj name:@"pic[]" fileName:key mimeType:@"image/jpeg"];
        }];


    } success:success failure:failure];

//    [self POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//
//        [dataParameters enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//            [formData appendPartWithFileData:obj name:@"pic[]" fileName:key mimeType:@"image/jpeg"];
//        }];
//
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//
//    } success:success failure:failure];

}


@end
